﻿
Imports System
Imports System.IO.Ports
Imports System.Threading


Public Class Form1

    Public Declare Function timeGetTime _
Lib "winmm.dll" () As Long

    Private mHeight As Long, mWidth As Long

  






    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        REM STR1 = "1b,80,ke,1b,81,R,1b,81,ea,30,05,11,1b,81,eb,30,05,11,1b,83"
        REM STR2 = "$1b$2b$09"

        SerialPort1.ReadTimeout = 1500
        ProgressBar1.Value = ProgressBar1.Minimum

        Dim TX1() As Byte = {&H1B, &H80, Asc("k"), Asc("e"), &H1B, &H81, Asc("R"), &H1B, &H81, Asc("e"), Asc("a"), Asc("3"), Asc("0"), Asc("0"), Asc("5"), Asc("1"), Asc("1"), &H1B, &H81, Asc("e"), Asc("b"), Asc("3"), Asc("0"), Asc("0"), Asc("5"), Asc("1"), Asc("1"), &H1B, &H83}
        Dim TX2() As Byte = {&H1B, &H2B, &H9}
        Dim c As String
        Dim tx_end, wait_end As Boolean
        Dim start_day, start_mount, start_year, end_day, end_mount, end_year As String
        Dim readValue, Desktop_Path, testValue As String
        readValue = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "Desktop", Nothing)
        REM testValue = My.Computer.Registry.Users. 

        If readValue = "Nothing" Then
            Desktop_Path = "c:\KLEN.txt"
        Else
            Desktop_Path = readValue & "\KLEN.txt"
        End If


        start_day = DateTimePicker1.Value.Day
        If Len(start_day) = 1 Then
            TX1(11) = Asc("0")
            TX1(12) = Asc(start_day)
        Else
            TX1(11) = Asc(Mid(start_day, 1, 1))
            TX1(12) = Asc(Mid(start_day, 2, 1))
        End If
        start_mount = DateTimePicker1.Value.Month

        If Len(start_mount) = 1 Then
            TX1(13) = Asc("0")
            TX1(14) = Asc(start_mount)
        Else
            TX1(13) = Asc(Mid(start_mount, 1, 1))
            TX1(14) = Asc(Mid(start_mount, 2, 1))
        End If

        start_year = DateTimePicker1.Value.Year
        TX1(15) = Asc(Mid(start_year, 3, 1))
        TX1(16) = Asc(Mid(start_year, 4, 1))






        end_day = DateTimePicker2.Value.Day
        If Len(end_day) = 1 Then
            TX1(21) = Asc("0")
            TX1(22) = Asc(end_day)
        Else
            TX1(21) = Asc(Mid(end_day, 1, 1))
            TX1(22) = Asc(Mid(end_day, 2, 1))
        End If

        end_mount = DateTimePicker2.Value.Month


        If Len(start_mount) = 1 Then
            TX1(23) = Asc("0")
            TX1(24) = Asc(end_mount)
        Else
            TX1(23) = Asc(Mid(end_mount, 1, 1))
            TX1(24) = Asc(Mid(end_mount, 2, 1))
        End If


        end_year = DateTimePicker2.Value.Year
        TX1(25) = Asc(Mid(start_year, 3, 1))
        TX1(26) = Asc(Mid(start_year, 4, 1))



        SerialPort1.BaudRate = 9600
        SerialPort1.Handshake = IO.Ports.Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.GetEncoding(1251)

        Try
            SerialPort1.Open()
        Catch ex As Exception
            MessageBox.Show("Портът е зает!")
            Exit Sub
        End Try


        SerialPort1.Write(TX1, 0, TX1.Length)
        SerialPort1.Write(TX2, 0, TX2.Length)


        Label1.Text = "Четене..."
        Label1.Refresh()


        Delay(500)

        SerialPort1.BaudRate = 115200
        SerialPort1.Handshake = IO.Ports.Handshake.None
        tx_end = True
        'Dim FILE_NAME As String = "C:\Users\Owner\Documents\test2.txt"
        'Dim oFile As System.IO.File
        Dim line1, line2 As String
        Dim oWrite As System.IO.StreamWriter
        oWrite = IO.File.CreateText(Desktop_Path)     '  "c:\test.txt"

        line1 = ""
        Dim start_line, Esc, Empty, Err_Device As Boolean
        Dim k, a As Integer
        start_line = False
        Empty = True
        Esc = False
        Err_Device = False
        a = 1

        Do While (tx_end)

            Try
                c = SerialPort1.ReadChar()
            Catch ex As TimeoutException
                MessageBox.Show("Не отговаря устройството")
                Err_Device = True
                Exit Do
            End Try

            If (wait_end) Then
                If (c = Asc("F")) Then
                    tx_end = False
                ElseIf c = Asc("P") Then
                    start_line = True
                    Empty = False
                End If
            End If

            If (c = &H9) Then
                wait_end = True
            Else : wait_end = False
            End If


            If start_line Then
                If c > 27 Then
                    line1 = line1 + ChrW(c)
                End If
            End If

            If Esc Then
                If (c = 115) Then
                    k = Len(line1)
                    If k > 0 Then
                        line1 = Mid(line1, 3, k - 4)
                    End If
                    line1 = line1 + vbCr + vbNewLine
                    start_line = False
                    oWrite.Write(line1)
                    line1 = ""

                    ProgressBar1.Value = ProgressBar1.Value + a
                    If ProgressBar1.Value = ProgressBar1.Maximum Then
                        a = -1
                    End If
                    If ProgressBar1.Value = ProgressBar1.Minimum Then
                        a = 1
                    End If

                End If
            End If



            If (c = &H1B) Then
                Esc = True
            Else : Esc = False
            End If
        Loop

        oWrite.Close()




        If (Err_Device) Then
            Label1.Text = "Не можах да го прочета"
        ElseIf (Empty) Then
            Label1.Text = "Празно е :("
        Else : Label1.Text = "Прочетох го :)"
        End If
        Label1.Refresh()

        ' ProgressBar1.Value = ProgressBar1.Minimum


        Delay(100)

        SerialPort1.Close()
        SerialPort1.BaudRate = 9600
        SerialPort1.Handshake = IO.Ports.Handshake.None

    End Sub
    
    







    Public Sub Delay(ByVal DelayInMiliseconds As Single)
        Dim TimeOut As Single
        TimeOut = DelayInMiliseconds + timeGetTime()
        Do Until (timeGetTime() >= TimeOut)
            REM(Events())

        Loop
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink
        Me.MaximizeBox = Windows.Forms.AutoSizeMode.GrowAndShrink
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = 200
        ProgressBar1.Style = ProgressBarStyle.Continuous
        ProgressBar1.Step = 0


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim testmsg As Integer
        Dim c As String
        Dim tx_end, wait_end, err_device, start_message As Boolean
        Dim TX3() As Byte = {&H1B, &H80, &H61, &H62, &H1B, &H83}
        Dim TX4() As Byte = {&H1B, &H2B, &H8} REM ESC+08
        testmsg = MsgBox("Сигурни ли сте ? ", 1)

        If testmsg = 1 Then

            SerialPort1.ReadTimeout = 1500
            ProgressBar1.Value = ProgressBar1.Minimum
            SerialPort1.BaudRate = 9600
            SerialPort1.Handshake = IO.Ports.Handshake.None
            SerialPort1.Encoding = System.Text.Encoding.GetEncoding(1251)
            SerialPort1.Handshake = IO.Ports.Handshake.None

            Try
                SerialPort1.Open()
            Catch ex As Exception
                MessageBox.Show("Портът е зает!")
                Exit Sub
            End Try

            SerialPort1.Write(TX3, 0, TX3.Length)
            SerialPort1.Write(TX4, 0, TX4.Length)
            Delay(100)

           
            SerialPort1.Close()

            MessageBox.Show("Дневно приключване")

        End If





    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim testmsg As Integer
        Dim c As String
        Dim tx_end, wait_end, err_device, start_message As Boolean
        Dim TX3() As Byte = {&H1B, &H80, &H69, &H69, &H1B, &H83}
        Dim TX4() As Byte = {&H1B, &H2B, &H8} REM ESC+08
        testmsg = MsgBox("Сигурни ли сте ? ", 1)

        If testmsg = 1 Then

            SerialPort1.ReadTimeout = 1500
            ProgressBar1.Value = ProgressBar1.Minimum
            SerialPort1.BaudRate = 9600
            SerialPort1.Handshake = IO.Ports.Handshake.None
            SerialPort1.Encoding = System.Text.Encoding.GetEncoding(1251)
            SerialPort1.Handshake = IO.Ports.Handshake.None

            Try
                SerialPort1.Open()
            Catch ex As Exception
                MessageBox.Show("Портът е зает!")
                Exit Sub
            End Try

            SerialPort1.Write(TX3, 0, TX3.Length)
            SerialPort1.Write(TX4, 0, TX4.Length)
            Delay(100)

           

            SerialPort1.Close()

            MessageBox.Show("Затваряне на бон ")

        End If


    End Sub
End Class
